package com.young.bs.vote.service.impl;

import com.young.common.util.CommonUtil;
import com.young.common.core.dal.service.IDataAccessService;
import com.young.common.util.Page;
import com.young.bs.vote.model.VotePerson;
import com.young.bs.vote.service.IVotePersonService;
import com.young.common.util.StringUtils;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
* 投票人服务实现
* Created by xxx on 2019-4-22.
*/
@Service("votePersonService")
public class VotePersonServiceImpl implements IVotePersonService {

    @Resource(name = "dataAccessService")
    IDataAccessService dataAccessService;//数据层服务

    private static final Logger logger = LoggerFactory.getLogger(VotePersonServiceImpl.class);

    private static final String MAPPER = "com.young.bs.vote.mapper.votePerson";//mapper的namespace

    //获取数据列表
    @Override
    public List<VotePerson> getList(VotePerson obj) {
        return dataAccessService.getList(MAPPER + ".getList", obj);
    }

    /**
     * 判断用户是否有指定投票的权力
     *
     * @param voteId
     * @param userId
     * @return
     */
    @Override
    public boolean canGoVote(String voteId, String userId) {
        VotePerson obj = new VotePerson();
        obj.setVoteId(voteId);
        obj.setUserId(userId);
        return this.count(obj) > 0;
    }

    //获取数据列表(分页)
    @Override
    public List<VotePerson> getPage(VotePerson obj, Page page) {
        return dataAccessService.getList(MAPPER + ".getList", obj, page);
    }

    //查询单条
    @Override
    public VotePerson get(String id) {
        return dataAccessService.getObject(MAPPER + ".get", id);
    }

    //统计数量
    @Override
    public int count(VotePerson obj) {
        return dataAccessService.getObject(MAPPER + ".count", obj);
    }

    //修改
    @Override
    public int update(VotePerson obj) {
        return dataAccessService.update(MAPPER + ".update", obj);
    }

    //批量删除
    @Override
    public int delete(String[] idArr) {
        int i = 0;
        for(String id : idArr){
            i += this.delete(id);
        }
        return i;
    }

    //删除单条
    @Override
    public int delete(String id) {
        return dataAccessService.delete(MAPPER + ".delete", id);
    }

    //插入
    @Override
    public int insert(VotePerson obj) {
        if (StringUtils.isBlank(obj.getId())) {
            obj.setId(CommonUtil.getUUID());
        }
        return dataAccessService.insert(MAPPER + ".insert", obj);
    }

    /**
     * 批量插入
     *
     * @param voteId
     * @param userIds
     * @return
     */
    @Override
    public int batchInsert(String voteId, String[] userIds) {
        for (String userId : userIds) {
            VotePerson votePerson = new VotePerson();
            votePerson.setVoteId(voteId);
            votePerson.setUserId(userId);
            List<VotePerson> list = this.getList(votePerson);
            if (list == null || list.size() == 0) {
                this.insert(votePerson);
            }
        }
        return 0;
    }

    //批量插入
    @Override
    public int batchInsert(List<VotePerson> list) {
        int i = 0;
        for(VotePerson item : list){
            i += this.insert(item);
        }
        return i;
    }

}